import {
  Component,
  Input,
  Output,
  EventEmitter,
  OnInit,
  OnChanges
} from "@angular/core";
import { NzNotificationService } from "ng-zorro-antd";
import { HttpServcie } from "../../../../../service/http.service";
import { Config } from "../../../../../service/config";
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from "@angular/forms";
import { Observable } from "rxjs";
import { PlanDictBean } from "../../plan-dict.bean";
import * as moment from "moment";
import { Router } from "@angular/router";

@Component({
  selector: "plan-dict-mod",
  templateUrl: "plan-dict-mod.html",
  styleUrls: ["plan-dict-mod.scss"]
})
export class PlanDictModPage implements OnInit, OnChanges {
  validateForm: FormGroup;
  isSpinning: boolean = false;
  @Output("outPut")
  outPut = new EventEmitter();
  @Input()
  inValue: any = [];
  config: any = Config;
  //系统类型
  planDictBean: PlanDictBean = new PlanDictBean();
  roleTypeList = [];
  _useroptions = []; //角色类型
  _cardTypeoption = [];
  partyList = [];
  user: any = {};

  constructor(
    private fb: FormBuilder,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private router: Router
  ) {}

  ngOnInit() {
    this.validateForm = this.fb.group({
      pdName: [null, [Validators.required]], //用户姓名
      pdType: [null, [Validators.required]], //用户持证类型
      pPartyName: [null, [Validators.required]], //证件号
      pUserName: [null, [Validators.required]], //身份证号码
      pStartDt: [null, [Validators.required]], //单位全称
      pEndDt: [null, [Validators.nullValidator]] //办公电话
    });

    this.user = JSON.parse(window.localStorage.getItem("user"));
  }

  ngOnChanges() {
    this.planDictBean = this.inValue;
    this.planDictBean.pStartDt = new Date(
      moment(this.planDictBean.pStartDt).format("YYYY-MM-DD")
    );
    this.planDictBean.pEndDt = new Date(
      moment(this.planDictBean.pEndDt).format("YYYY-MM-DD")
    );
  }

  submitForm($event) {
    //校验是否能提交
    //没有填的标红
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }
    //处理是否提交
    for (const i in this.validateForm.controls) {
      if (!this.validateForm.controls[i].valid) {
        return;
      }
    }
    this.isSpinning = true;

    this.planDictBean.pStartDt = moment(this.planDictBean.pStartDt).format(
      "YYYYMMDD"
    );
    this.planDictBean.pEndDt = moment(this.planDictBean.pEndDt).format(
      "YYYYMMDD"
    );

    let ob = this.httpServcie.updatePlanDict(this.planDictBean);
    ob.subscribe(data => {
      this.isSpinning = false;
      if (data.status == 200) {
        this._notification.create("success", "成功提示", "修改成功");
        this.outPut.emit($event);
      } else if (data.status == 401) {
        this._notification.create("error", "失败", "请重新登录");
        this.router.navigateByUrl("/login");
      } else {
        this._notification.create("error", "失败提示", data.message);
        return;
      }
    });
  }

  //交验
  getFormControl(name) {
    return this.validateForm.controls[name];
  }
}
